import 'package:flutter/material.dart';
import 'profile_page.dart';
import 'fit_analysis_page.dart';
import 'settings_page.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // 将个人信息状态提升到 HomePage，以便于管理和传递
  double height = 170.0;
  double weight = 65.0;
  String gender = '男性';
  DateTime? birthDate;

  // 更新个人信息的回调方法
  void _updatePersonalInfo(Map<String, dynamic> data) {
    setState(() {
      height = data['height'];
      weight = data['weight'];
      gender = data['gender'];
      birthDate = data['birthDate'];
    });
  }

  // 导航到设置页面
  Future<void> _navigateToSettings() async {
    final result = await Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => SettingsPage(
          height: height,
          weight: weight,
          gender: gender,
          birthDate: birthDate,
        ),
      ),
    );

    if (result != null && result is Map<String, dynamic>) {
      _updatePersonalInfo(result);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('健康数据中心'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: ListView(
        padding: const EdgeInsets.all(16.0),
        children: [
          _buildMenuItem(
            context,
            icon: Icons.person,
            title: '个人数据',
            subtitle: '查看和编辑您的个人健康指标',
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => ProfilePage(
                    height: height,
                    weight: weight,
                    gender: gender,
                    birthDate: birthDate,
                    onSettingsNavigate: _navigateToSettings, // 传递导航方法
                  ),
                ),
              );
            },
          ),
          _buildMenuItem(
            context,
            icon: Icons.directions_bike,
            title: 'FIT 数据分析',
            subtitle: '分析您的骑行或跑步 FIT 文件',
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => const FitAnalysisPage()),
              );
            },
          ),
          // 您也可以用同样的方法，将 OCR 相关的功能拆分成一个独立的页面
          // _buildMenuItem(
          //   context,
          //   icon: Icons.document_scanner,
          //   title: 'OCR 数据解析',
          //   subtitle: '从图片识别健康数据',
          //   onTap: () {
          //     // Navigator.push(context, MaterialPageRoute(builder: (context) => OcrPage()));
          //   },
          // ),
        ],
      ),
    );
  }

  Widget _buildMenuItem(BuildContext context,
      {required IconData icon,
      required String title,
      required String subtitle,
      required VoidCallback onTap}) {
    return Card(
      elevation: 2,
      margin: const EdgeInsets.only(bottom: 16),
      child: ListTile(
        leading: Icon(icon, size: 40, color: Theme.of(context).primaryColor),
        title: Text(title, style: Theme.of(context).textTheme.titleLarge),
        subtitle: Text(subtitle),
        trailing: const Icon(Icons.arrow_forward_ios),
        onTap: onTap,
      ),
    );
  }
}
